package com.blog.web.login.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.blog.consts.AppConsts;

/**
 *
 * 文件名： com.blog.web.login.controller.CodeController.java
 * 作者：   WenBin
 * 日期：   2017年4月13日
 * 功能说明：
 * =========================================================
 * 修改记录：
 * 修改作者    日期      修改内容
 * =========================================================
 *  Copyright (c) 2010-2011 .All rights reserved.
 */
@Controller
@RequestMapping(value = "/code")
public class CodeController {

	private int width = 90;//验证码宽度
	private int height = 25;//验证码高度
	private int codeCount = 4;//验证码个数
	private int lineCount = 19;//混淆线个数

	char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
			'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
			'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

	/**
	 *
	 * @Method: com.blog.web.login.controller.CodeController.getCode
	 * @Description: 二维码
	 * @author: WenBin
	 * @date: 2017年4月14日
	 * @version: 1.0
	 * @param time
	 * @param request
	 * @param response
	 * @throws IOException
	 * void
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping(value = "/validate/{time}")
	public void getCode(@PathVariable("time") String time, HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		//定义随机数类
		Random r = new Random();
		//定义存储验证码的类
		StringBuilder builderCode = new StringBuilder();
		//定义画布
		BufferedImage buffImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
		//得到画笔
		Graphics g = buffImg.getGraphics();
		//1.设置颜色,画边框
		g.setColor(Color.black);
		g.drawRect(0,0,width,height);
		//2.设置颜色,填充内部
		g.setColor(Color.white);
		g.fillRect(1,1,width-2,height-2);
		//3.设置干扰线
		g.setColor(Color.gray);
		for (int i = 0; i < lineCount; i++) {
			g.drawLine(r.nextInt(width),r.nextInt(width),r.nextInt(width),r.nextInt(width));
		}
		//4.设置验证码
		g.setColor(Color.blue);
		//4.1设置验证码字体
		g.setFont(new Font("宋体",Font.BOLD|Font.ITALIC,15));
		for (int i = 0; i < codeCount; i++) {
			char c = codeSequence[r.nextInt(codeSequence.length)];
			builderCode.append(c);
			g.drawString(c+"",15*(i+1),15);
		}
		//6.保存到session中
		HttpSession session = request.getSession();
		session.setAttribute(AppConsts.LOGIN_CODE,builderCode.toString());
		//5.输出到屏幕
		ServletOutputStream sos = response.getOutputStream();
		ImageIO.write(buffImg,"png",sos);

		//7.禁止图像缓存。
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/png");
		//8.关闭sos
		sos.close();
	}
}
